<?php


namespace ServiceSdk\Payment\Payunk;


use ServiceSdk\Payment\BasePayment;

class Config extends BasePayment
{
    /**
     * 畅联支付配置
     * 官网 https://cl.payunk.com/user/api/index.html
     * @return array
     */
    protected static function payunkConfig(): array
    {
        return [
            'appKey'                   => '1028919',
            'secretKey'                => 'OpXOpvXcgg6jSCz2ZovMFrkNUU8OYZ44',
            //                        'apiUrl'    => 'https://api2.payunk.com/index/unifiedorder?format=json',
            'createPayAlipayOrderUrl'     => 'https://api.payunk.com/index/unifiedorder?format=json',
            'createWechatOrderUrl'     => 'https://api2.payunk.com/index/unifiedorder?format=jsonIn',
            'createWechatJsshOrderUrl' => 'https://api.payunk.com/index/unifiedorder?format=json',
            'checkOrderUrl'            => 'https://api2.payunk.com/index/getorder',
            'cancelOrderUrl'           => 'https://api2.payunk.com/index/cancelOrder',
            'version'                  => 'v1.0',
        ];
    }

    /**
     * 加密数据，生成加密sign
     *
     * @param array $data
     *
     * @return string
     */
    protected static function getSign(array $data): string
    {
        $payConfig = self::payunkConfig();
        // 去空
        $data = array_filter($data);
        //签名步骤一：按字典序排序参数
        ksort($data);
        if (isset($data['pay_type']) && $data['pay_type'] == 'AliRoyalty') {
            foreach ($data['royalty_parameters'] as $k => $v) {
                ksort($data['royalty_parameters'][$k]);
            }
        }
        $ksortString = http_build_query($data);
        $ksortString = urldecode($ksortString);
        //签名步骤二：在string后加入mch_key
        $string_sign_temp = $ksortString . "&key=" . $payConfig['secretKey'];
        //签名步骤三：MD5加密
        $sign = md5($string_sign_temp);
        // 签名步骤四：所有字符转为大写
        $result = strtoupper($sign);

        return $result;
    }

}
